/* $Id:game_window.cc jjs $ */
/*
   Copyright (C) 2007 by Jurjen Stellingwerff <jurjen@stwerff.xs4all.nl>
   Part of the Moros Project http://moros.sourceforge.net/

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License.
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY.

   See the COPYING file for more details.
*/

#define DEBUG_ROUTINE "game_window"

#include "moros.h"
#include "game_window.h"
#include "picture.h"
#include "../general/log.h"
#include "../general/util.h"

game_window::game_window(BaseObjectType* cobject, const Glib::RefPtr<Gnome::Glade::Xml> refGlade)
: Gtk::Window(cobject), m_refGlade(refGlade) {
	m_refGlade->get_widget_derived("game_area", area);
	area->curr=this;
	m_refGlade->get_widget("game_tree", game_tree);
	sigc::signal<void, channel*> game_handler_signal;
	game_handler_signal.connect(sigc::mem_fun(this, &game_window::game_handler));
	//ch=game_data->comm.client("127.0.0.1", 11000, game_handler_signal);
	//ch->write("refresh", "1000,1000");
	//ch->send();
}

game_window::~game_window() {
}

/// Screen = 11*15
/// Unit 0 movement = current user
/// Find click on window

void game_window::set_map(int x, int y, tile t) {
    int mx=x-cx+slen_x*3;
    int my=y-cy+slen_y*3;
    if (mx<0 || my<0 || my>slen_x*6+1 || my>slen_y*6+1) return;
    map[mx][my]=t;
}
/*
    change background picture on tile change
    check for changes in foregroup picture (check if it is inside focus)
    void 	queue_draw_area (int x, int y, int width, int height)
 	Invalidates the rectangular area of widget defined by x , y , width and height by calling gdk_window_invalidate_rect() on the widget's window and all its child windows.
*/
tile game_window::get_map(int x, int y) {
    tile t;
    int mx=x-cx+slen_x*3;
    int my=y-cy+slen_y*3;
    if (mx<0 || my<0 || my>slen_x*6+1 || my>slen_y*6+1) return t;
    return map[mx][my];
}

void game_window::game_handler(channel *ch) {
    while(true) {
        if (ch->key=="tile") {
            pos p(ch->value);
            tile t;
            ch->next();
            if (ch->key=="t") {
                int a;
                t.terrain=toint(ch->value);
                ch->next();
                for (a=0; a<8; a++) {
                    t.add[a]=0;
                }
            }
            while (ch->key=="a") {
                int a;
                for(a=0; a<8; a++) {
                    if (t.add[a]!=0) {
                        t.add[a]=toint(ch->value);
                        break;
                    }
                }
                ch->next();
            }
            if (ch->key=="f") {
                t.fields=toint(ch->value);
                ch->next();
            }
            if (ch->key=="city") {
                t.name=ch->value;
                ch->next();
                if (ch->key=="sz") {
                    t.citysize=toint(ch->value);
                    ch->next();
                }
                if (ch->key=="wsz") {
                    t.wallsize=toint(ch->value);
                    ch->next();
                }
            } else if (ch->key=="castle") {
                t.name=ch->value;
                ch->next();
                if (ch->key=="sz") {
                    t.castlesize=toint(ch->value);
                }
            }
            if (ch->key=="w") {
                t.wall=toint(ch->value);
                ch->next();
            }
            if (ch->key=="m") {
                t.moat=toint(ch->value);
                ch->next();
            }
            if (ch->key=="c") {
                t.castlesize=toint(ch->value);
                ch->next();
            }
            set_map(p.x, p.y, t);
        }
        if (ch->key=="unit") {
        }
        if (!ch->next()) break;
    }
}
/* mouse click
bool 	translate_coordinates (Widget& dest_widget, int src_x, int src_y, int& dest_x, int& dest_y)
 	Translate coordinates relative to src_widget 's allocation to coordinates relative to dest_widget 's allocations.
*/
/*
measurements: 11
  x = 7*(36+72)+36 = 792
  y = 11*72 = 792
*/

#define bg_width 792
#define bg_height 792

void draw_area::bg_image(string name, int x, int y) {
    Glib::RefPtr<Gdk::Pixbuf> image=game_data->img_cache[name].image;
    image->render_to_drawable(background, get_style()->get_black_gc(), 0, 0, x, y, image->get_width(), image->get_height(), Gdk::RGB_DITHER_NONE, 0, 0);
}

void draw_area::calc_background() {
    Glib::RefPtr<Gdk::Window> window = get_window();
    background=Gdk::Pixmap::create(window, bg_width, bg_height);
    bg_image("terrain/coast.png", 200, 116);
}

void draw_area::move_background(int dir) {
    // define new background while remembering old one
    // draw part of the old one on the new one
}

void draw_area::image(string name, int x, int y) {
    Glib::RefPtr<Gdk::Pixbuf> image=game_data->img_cache[name].image;
    image->render_to_drawable(get_window(), get_style()->get_black_gc(), 0, 0, x, y, image->get_width(), image->get_height(), Gdk::RGB_DITHER_NONE, 0, 0);
}

bool draw_area::on_expose_event(GdkEventExpose* event) {
    int w, h;
    background->get_size (w, h);
    get_window()->draw_drawable(get_style()->get_black_gc(), background, 0, 0, 0, 0, w, h);
    image("terrain/grass1.png", 100, 80);
    return true;

    // Glib::Ref

    // set_font_face(), set_font_size()
    // context save()/restore() ???  save current settings and restore them to the last point
    //event->area.x, event->area.y, event->area.width, event->area.height


    // draw_pixbuf(gc, pixbuf, ix, iy, x, y, w, h, dither, xd, yd)
    // Gdk::Drawable


}
